<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;

use App\Models\Config as ConfigModel;

use DateTimeInterface;//时间格式化

class OrderProduct extends Model
{
    protected $table = 'order_product';

    protected $guarded = [];

    protected $dateFormat = 'U';

    const CREATED_AT = null;

    const UPDATED_AT = 'updatetime';

    protected $appends = [
        'proimg_list', // 多张
    ];

    public function getProimgListAttribute()
    {
        $url = ConfigModel::where('name', 'shopurl')->value('value');

        $url = env('SITE_URL', $url);

        $proimg = !empty($this->attributes['proimg']) ? $this->attributes['proimg'] : '';

        if (!empty($proimg)) {

            $proimg  = str_replace('/uploads/', $url . '/uploads/', $proimg);

            $proimg = explode(',', $proimg);

            return $proimg;
        } else {
            return  [];
        }
    }

    public function product()
    {
        return $this->belongsTo('App\Models\Product\Product', 'proid', 'id');
    }


    protected function serializeDate(DateTimeInterface $date)//序列化日期
    {
        return $date->format('Y-m-d H:i:s');
    }
}